<html>
<head><meta charset="utf-8"><title>Padding and niches · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html">Padding and niches</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="191178107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191178107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191178107">(Mar 19 2020 at 21:32)</a>:</h4>
<p>So reading <a href="https://www.reddit.com/r/rust/comments/fimbo3/reducing_the_size_of_optiont_by_adding_members_to/" target="_blank" title="https://www.reddit.com/r/rust/comments/fimbo3/reducing_the_size_of_optiont_by_adding_members_to/">this</a> made me wonder about padding and niches again... I wonder if it would make sense to add a <code>repr</code> attribute that basically says "for this type, instead of padding with <em>arbitrary</em> bytes, please pad with <em>guaranteed zero</em> bytes". So basically, the compiler adds padding fields of appropriate size that only have a single valid value, 0x00. That would have the downside that these bytes must be copied when the struct is copied (unlike padding), but it also means that space could be used for niche optimizations.</p>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> probably already opened an issue for doing this 3 years ago, but at least I haven't seen it. ;)</p>



<a name="191178143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191178143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191178143">(Mar 19 2020 at 21:33)</a>:</h4>
<p>I think this is also the direction <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/174#issuecomment-599160855" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/174#issuecomment-599160855">these comments</a> take</p>



<a name="191180417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191180417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191180417">(Mar 19 2020 at 21:56)</a>:</h4>
<p>I have not, I don't think</p>



<a name="191180449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191180449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191180449">(Mar 19 2020 at 21:56)</a>:</h4>
<blockquote>
<p>That would have the downside that these bytes must be copied when the struct is copied (unlike padding)</p>
</blockquote>
<p>note that this is only a thing for non-<code>Aggregate</code> ABI <code>struct</code>s</p>



<a name="191180512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191180512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191180512">(Mar 19 2020 at 21:57)</a>:</h4>
<p>LLVM doesn't know about padding in our <code>struct</code>s, it has no mechanism through which to indicate them (other then maybe first-class aggregates but we stopped using those except for pair return values)</p>



<a name="191180832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191180832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191180832">(Mar 19 2020 at 22:00)</a>:</h4>
<p>the only change I could see being needed is <code>SetDiscriminant</code> zeroing out the padding (although it would really only need to zero out the niche, optimization-wise :P)</p>



<a name="191180904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191180904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191180904">(Mar 19 2020 at 22:01)</a>:</h4>
<p>then again such a layout would offer multiple niches if applied to <code>enum</code>s</p>



<a name="191180962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191180962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191180962">(Mar 19 2020 at 22:01)</a>:</h4>
<p>but we gave up on using multiple niches because we expected we'd never zero out padding by default and didn't think of the opt-in, so it would take more effort now</p>



<a name="191333336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191333336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191333336">(Mar 21 2020 at 07:37)</a>:</h4>
<p>I see. So should I open a new issue for this, or is there some general niche-related issue I could add this to?</p>



<a name="191351161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191351161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191351161">(Mar 21 2020 at 15:34)</a>:</h4>
<p>I opened an issue: <a href="https://github.com/rust-lang/rust/issues/70230" target="_blank" title="https://github.com/rust-lang/rust/issues/70230">https://github.com/rust-lang/rust/issues/70230</a></p>



<a name="191807811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191807811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tilpner <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191807811">(Mar 25 2020 at 20:37)</a>:</h4>
<p>Another usecase that could benefit from guaranteed-initialised padding are eBPF programs inside the Linux kernel.<br>
They are being statically verified, to ensure that no uninitialised memory reaches "foreign" functions.</p>
<p>It is currently tricky to pass Rust structs and enums to other Rust programs through these "foreign" functions, because the verifier<br>
notices that e.g. Option&lt;u32&gt; contains uninitialised padding. It is sometimes possible to work around this, by manually zeroing a struct/enum,<br>
and then setting its fields directly, without copying over the padding, but that is unergonomic.</p>



<a name="191851886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191851886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191851886">(Mar 26 2020 at 08:33)</a>:</h4>
<p><span class="user-mention" data-user-id="277470">@tilpner</span> so... the Rust program is compiled to eBPF, and then that program is statically verified by the kernel in certain ways? that's interesting, is that analysis described in detail somewhere?</p>



<a name="191870657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/191870657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#191870657">(Mar 26 2020 at 11:30)</a>:</h4>
<p><a href="https://lwn.net/Articles/740157/" title="https://lwn.net/Articles/740157/">https://lwn.net/Articles/740157/</a>, <a href="https://github.com/torvalds/linux/blob/master/Documentation/networking/filter.txt" title="https://github.com/torvalds/linux/blob/master/Documentation/networking/filter.txt">https://github.com/torvalds/linux/blob/master/Documentation/networking/filter.txt</a></p>



<a name="192113863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Padding%20and%20niches/near/192113863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Padding.20and.20niches.html#192113863">(Mar 28 2020 at 09:13)</a>:</h4>
<p>that seems to mostly be about uninitialized "registers", so compiling Rust to eBPF ends up putting enums directly in registers?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>